package com.dokimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dokimall.product.entity.ProductAttrValueEntity;
import com.dokimall.product.service.ProductAttrValueService;
import com.dokimall.product.vo.AttrRespVo;
import com.dokimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dokimall.product.service.AttrService;
import com.dokimall.common.utils.PageUtils;
import com.dokimall.common.utils.R;



/**
 * 商品属性
 *
 * @author mori
 * @email 550144898@qq.com
 * @date 2021-09-26 15:02:04
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId")Long catelogId,
                      @PathVariable("attrType")String attrType){
        PageUtils page = attrService.queryPageBaseAttrPage(params,catelogId,attrType);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		// AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrRespById(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		//attrService.save(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		// attrService.updateById(attr);
        attrService.updateByAttrVo(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    /**
     * /product/attr/base/listforspu/{spuId}
     * 获取spu规格
     * */
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId")Long spuId){

        List<ProductAttrValueEntity> data = productAttrValueService.listForSpu(spuId);

        return R.ok().put("data",data);
    }

    /**
     * /update/{spuId}
     * 修改商品规格
     * */
    @PostMapping("/update/{spuId}")
    public R updataSpu(@PathVariable("spuId")Long spuId, @RequestBody List<ProductAttrValueEntity> data){
        productAttrValueService.updateBySpuId(spuId,data);
        return R.ok();
    }


}
